package com.kelaocai.motto.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.kelaocai.motto.entity.Motto;
import com.kelaocai.motto.utility.DBAdapter;

public class MottosDao extends DBAdapter {

	private static final String TAG = "MottosDao";

	private static final String DATABASE_TABLE = "Mottos";

	public MottosDao(Context ctx) {
		super(ctx);
		// TODO Auto-generated constructor stub
	}

	public List<Motto> getAllMottos() {

		super.open();

		Cursor cursor;
		List<Motto> motto_list = new ArrayList<Motto>();

		Log.i(TAG, "query all mottos records");
		cursor = db.query(DATABASE_TABLE, new String[] { "_id", "type",
				"author", "content_ch", "content_en", "status", "comments",
				"createdate", "custom" }, null, null, null, null, null);

		while (cursor.moveToNext()) {
			Motto motto = new Motto();
			motto.set_id((cursor.getInt(cursor.getColumnIndex("_id"))));
			motto
					.setAuthor((cursor.getString(cursor
							.getColumnIndex("author"))));
			motto.setContent_ch((cursor.getString(cursor
					.getColumnIndex("content_ch"))));
			motto.setContent_en((cursor.getString(cursor
					.getColumnIndex("content_en"))));
			motto.setCreatedate((cursor.getString(cursor
					.getColumnIndex("createdate"))));
			motto.setComments((cursor.getString(cursor
					.getColumnIndex("comments"))));
			motto
					.setCustom((cursor.getString(cursor
							.getColumnIndex("custom"))));
			motto
					.setStatus((cursor.getString(cursor
							.getColumnIndex("status"))));
			motto.setType((cursor.getString(cursor.getColumnIndex("type"))));
			motto_list.add(motto);

		}

		super.close();
		return motto_list;
	}

	public List<Motto> getCustomMottos() {

		super.open();

		Cursor cursor;
		List<Motto> motto_list = new ArrayList<Motto>();

		Log.i(TAG, "query all mottos records");
		cursor = db.query(DATABASE_TABLE, new String[] { "_id", "type",
				"author", "content_ch", "content_en", "status", "comments",
				"createdate", "custom" }, " custom='1' ", null, null, null, null);

		while (cursor.moveToNext()) {
			Motto motto = new Motto();
			motto.set_id((cursor.getInt(cursor.getColumnIndex("_id"))));
			motto
					.setAuthor((cursor.getString(cursor
							.getColumnIndex("author"))));
			motto.setContent_ch((cursor.getString(cursor
					.getColumnIndex("content_ch"))));
			motto.setContent_en((cursor.getString(cursor
					.getColumnIndex("content_en"))));
			motto.setCreatedate((cursor.getString(cursor
					.getColumnIndex("createdate"))));
			motto.setComments((cursor.getString(cursor
					.getColumnIndex("comments"))));
			motto
					.setCustom((cursor.getString(cursor
							.getColumnIndex("custom"))));
			motto
					.setStatus((cursor.getString(cursor
							.getColumnIndex("status"))));
			motto.setType((cursor.getString(cursor.getColumnIndex("type"))));
			motto_list.add(motto);

		}

		super.close();
		return motto_list;
	}

	public void updateCustom(List<Integer> list_custom,String flag) {
		super.open();
		//db.beginTransaction();
		for (int i = 0; i < list_custom.size(); i++) {
			db.execSQL("update mottos set custom='"+flag+"' where _id="+ list_custom.get(i).intValue());
			Log.i(TAG, "update:"+list_custom.get(i));
		}
		//db.endTransaction();
		super.close();

	}

}
